package com.phantasy.scene.sceneManager
{
	import com.phantasy.globals.GameParameters;
	import com.phantasy.ui.UISystem;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	/**
	 *全局轮询管理器 
	 * @author Liseen
	 * 
	 */	
	public class LoopManager 
	{
		
		private static var _currentFrame:int;
		private static var frameRates:Vector.<Number> = new Vector.<Number>(6);
		private static var frameLoopDic:Dictionary;
		private static var timer:Timer;
		private static var timeoutDic:Dictionary;
		private static var timeLoopDic:Dictionary;
		private static var delayIDKey:int;
		private static var secondTimer:Timer;
		private static var SecondLoopDic:Dictionary;
		public static var lastTime:int = getTimer();
		public static var currentTime:int = getTimer();
		
		
		public static function init(_arg1:Stage):void
		{
			frameLoopDic = new Dictionary();
			timeoutDic = new Dictionary();
			timeLoopDic = new Dictionary();
			SecondLoopDic = new Dictionary();
			_arg1.addEventListener(Event.ENTER_FRAME, frameLoop);
			timer = new Timer(80);
			timer.addEventListener(TimerEvent.TIMER, timerLoop);
			timer.start();
			secondTimer = new Timer(1000);
			secondTimer.addEventListener(TimerEvent.TIMER, secondLoop);
			secondTimer.start();
		}
		
		private static function frameLoop(e:Event):void
		{
			var s:Object;
			var f:Function;
			lastTime = currentTime;
			currentTime = getTimer();
			for (s in frameLoopDic)
			{
				f = frameLoopDic[s];
				if (GameParameters.getInstance().isDebug())
				{
					f.call();
				}
				else
				{
					try
					{
						f.call();
					}
					catch(e:Error)
					{
					}
				}
			}
			UISystem.upDate();
			doFrameRate();
		}
		
		private static function timerLoop(e:TimerEvent):void
		{
			var obj:Object;
			var s:Object;
			var f:Function;
			for each (obj in timeoutDic)
			{
				if ((getTimer() - obj.startTime) >= obj.count)
				{
					obj.handler.apply(null, obj.arg);
					obj.handler = null;
					timeoutDic[obj.key] = null;
					delete timeoutDic[obj.key];
				};
			};
			for (s in timeLoopDic)
			{
				f = timeLoopDic[s];
				if (GameParameters.getInstance().isDebug())
				{
					f.call();
				}
				else
				{
					try
					{
						f.call();
					}
					catch(e:Error)
					{
					};
				};
			};
		}
		
		private static function secondLoop(e:TimerEvent):void
		{
			var s:Object;
			var f:Function;
			for (s in SecondLoopDic)
			{
				f = SecondLoopDic[s];
				if (GameParameters.getInstance().isDebug())
				{
					f.call();
				}
				else
				{
					try
					{
						f.call();
					}
					catch(e:Error)
					{
					};
				};
			};
		}
		
		public static function addToFrame(_arg1:Object, _arg2:Function):void
		{
			if (frameLoopDic[_arg1] == null)
			{
				frameLoopDic[_arg1] = _arg2;
			};
		}
		
		public static function removeFromFrame(_arg1:Object):void
		{
			if (frameLoopDic[_arg1])
			{
				frameLoopDic[_arg1] = null;
				delete frameLoopDic[_arg1];
			};
		}
		
		public static function addToTimer(_arg1:Object, _arg2:Function):void
		{
			if (timeLoopDic[_arg1] == null)
			{
				timeLoopDic[_arg1] = _arg2;
			};
		}
		
		public static function removeFromTimer(_arg1:Object):void
		{
			if (timeLoopDic[_arg1])
			{
				timeLoopDic[_arg1] = null;
				delete timeLoopDic[_arg1];
			};
		}
		
		public static function addToSecond(_arg1:Object, _arg2:Function):void
		{
			if (SecondLoopDic[_arg1] == null)
			{
				SecondLoopDic[_arg1] = _arg2;
			};
		}
		
		public static function removeFromSceond(_arg1:Object):void
		{
			if (SecondLoopDic[_arg1])
			{
				SecondLoopDic[_arg1] = null;
				delete SecondLoopDic[_arg1];
			};
		}
		
		public static function setTimeout(_arg1:Function, _arg2:int, _arg3:Array=null):int
		{
			delayIDKey++;
			var _local4:Object = {
				"key":delayIDKey,
				"startTime":getTimer(),
				"count":_arg2,
				"handler":_arg1,
				"arg":_arg3
			};
			if (timeoutDic[delayIDKey] == null)
			{
				timeoutDic[delayIDKey] = _local4;
			};
			return (delayIDKey);
		}
		
		public static function clearTimeout(_arg1:int):void
		{
			if (timeoutDic[_arg1])
			{
				timeoutDic[_arg1] = null;
				delete timeoutDic[_arg1];
			};
		}
		
		public static function get realRate():Number
		{
			var _local1:Number = 0;
			var _local2:int;
			while (_local2 < frameRates.length)
			{
				_local1 = (_local1 + frameRates[_local2]);
				_local2++;
			};
			return ((_local1 / frameRates.length));
		}
		
		private static function doFrameRate():void
		{
			_currentFrame++;
			var _local1:int = getTimer();
			var _local2:Number = (1000 / (currentTime - lastTime));
			lastTime = _local1;
			if (frameRates.length < 6)
			{
				frameRates.push(_local2);
			}
			else
			{
				frameRates.shift();
				frameRates.push(_local2);
			}
		}
		
		public static function get currentFrame():int
		{
			return (_currentFrame);
		}
		
		
	}
}
